Skip to content

fix(migrate): 0.14 blob@, attach@ and filepath@ columns read in 2.x after migration - #1753

Open
dimitri-yatsenko wants to merge 6 commits into
masterfrom
fix/1698-migrate-014-external
Open

dimitri-yatsenko wants to merge 6 commits into
masterfrom
fix/1698-migrate-014-external

Conversation

@dimitri-yatsenko

@dimitri-yatsenko dimitri-yatsenko commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

After the documented migration of a 0.14 schema, columns declared blob@store, attach@store, or filepath@store could not be fetched in 2.x, although the migration reported success. This PR makes both documented paths write values that 2.x reads: Option A (copy into a 2.x schema) and Option B (in place). It also fixes the related defects listed in the issue, and two defects found along the way: create_parallel_schema could not copy a schema whose tables reference tables that sort after them, and garbage collection could delete migrated 0.14 files when a store uses the root-level layout.

Fixes #1698

Docs: datajoint/datajoint-docs#319

Changes

The 0.14 layout comes from external.py, hash.py, and declare.py. I checked it from v0.14.6 through v0.14.10: paths and hashes are the same in every release.

Metadata that 2.x reads

  • migrate_external writes {hash, path, schema, store, size}.
    • The path is where 0.14 stored the file: {database}/{subfolders}/{hex digest}. For attachments, .{attachment name} is appended. {database} is the database 0.14 wrote the file from. For a copy of a 0.14 schema, that is the original database, not the copy (see below).
    • Subfolding is the store's subfolding setting, or 0.14's default (2, 2) when the store has none. "subfolding": null, which the 2.x configuration template writes, counts as none. An explicit list wins, including [] for a 0.14 store that turned subfolding off.
    • Blobs that 0.14 had migrated from 0.11 keep an explicit path in filepath, and the hash of their contents in contents_hash. These are used, as 0.14 did.
    • The hash is the same MD5 digest, written in base32. 0.14 hashed the same bytes that 2.x hashes. So 2.x checks migrated values with its usual hash check. No check is skipped or relaxed.
  • Reading a migrated attachment. 0.14 stored only the contents in the file, but hashed the name, a NUL byte, and the contents. Those are the bytes <attach> stores. Migrated attachments record attachment_name, and get_hash puts the name back in front of the contents before checking the hash. This is 0.14's own hash, so a changed file or name fails the check.
  • migrate_filepath matches 0.14's binary(16) filepath columns. It writes {path, store, size, is_dir, timestamp} from the tracking table. From 0.14.7, filepath rows can have a NULL contents_hash (large files skip the checksum). These rows migrate like any other, because 2.x does not need it.
  • The column comment is written as :<blob@store>:, :<attach@store>:, or :<filepath@store>:, followed by the rest of the 0.14 comment.
  • Comments are quoted and escaped in every ALTER. A 0.14 comment with an apostrophe, a backslash, or a % now migrates and keeps its text.

Copies of a 0.14 schema

  • migrate_external, migrate_external_pointers_v2, copy_table_data, and verify_external_integrity take legacy_database: the name of the database 0.14 wrote the files from. It defaults to the schema being migrated (for copy_table_data, to source_schema). On a copy, such as one made with create_parallel_schema(..., copy_data=True), pass the original name. Without it, the files are not found and the call refuses with nothing changed; the error says to pass legacy_database. Filepath paths are stored in full in the tracking table, so migrate_filepath needs no such parameter.
  • create_parallel_schema creates tables in name order, so a table could come before the tables its foreign keys reference, such as the 0.14 ~external_* tables, which sort last. On master this fails with MySQL error 1824. It now turns foreign key checks off for its session while it creates and fills the tables, and restores the previous setting afterwards. The source rows already satisfy the constraints. References to tables in the same database are unqualified in SHOW CREATE TABLE, so the copy's foreign keys point to the copy's own tables (tested). Comments with % are now copied.

Option A: copy into a 2.x schema

  • copy_table_data converts in-store columns when it copies a 0.14 table into a table declared by 2.x. A 0.14 blob@store column must be declared <blob@store> there (likewise attach, filepath). The 2.x store must point to the 0.14 location. Files stay where they are. The rows are copied in one statement. Tables without 0.14 in-store columns are copied exactly as before.
  • migrate_external_pointers_v2 converts one 0.14 column in place, for a table still in the 0.14 layout. It adds a JSON column, fills it, and swaps it in, keeping the original as <name>_v1. The new comment names dest_store. On a column already declared by 2.x, it raises and points to copy_table_data. Before, it wrote JSON that 2.x read as raw bytes, or failed with Data too long.

Option B: in place

  • If a step fails, the _v2 columns the run wrote are dropped again, and the original columns are unchanged.
  • A rerun rewrites _v2 columns written by an earlier 2.x release, whose values 2.x cannot read. Before, it skipped them. It also repairs columns that an earlier release finalized, from the references kept in <name>_v1. Columns that already read are skipped.
  • Repair cannot restore NOT NULL. An earlier release's finalize made every column nullable and did not keep the 0.14 setting, so a repaired column stays nullable. The dry run marks these columns with nullable: True, and the run logs a warning. Where the column was required, restore it by hand in SQL, for example ALTER TABLE `db`.`recording` MODIFY COLUMN `signal` JSON NOT NULL COMMENT ':<blob@store>:raw signal' (tested), keeping the column's comment. alter() cannot do this while the table keeps its binary(16) <name>_v1 columns, because 2.x definitions do not accept that type.
  • Finalize checks first that every _v2 column exists and reads, and changes nothing otherwise. It renames both columns in one statement. The migrated column keeps the original's nullability. Before, a required column became optional.

Garbage collection

Migrated 0.14 files stay where 0.14 stored them, under {database}/. With the default schema_prefix (_schema), garbage collection never walks there. With schema_prefix="" (the root-level layout for stores written by 2.3.0 and earlier), {database}/ is the schema section, and on master GC lists the migrated files as orphans and collect(dry_run=False) deletes them. <filepath@> files may lie there too, and master does not read <filepath@> references. In the root-level layout only, GC now keeps these files:

  • a file that a hash reference (such as a migrated <blob@> or <attach@> value) or a <filepath@> reference points to counts as referenced;
  • a file named as 0.14 named blobs and attachments (32 hex digits, optionally followed by . and a name) is never an orphan;
  • a file directly under {schema}/, with no subfolder, is never an orphan. 2.x schema-addressed paths are {schema}/{table}/..., so such a file is a 0.14 file stored without subfolding or a blob that 0.14 had migrated from 0.11;
  • the section of a schema that still has 0.14 ~external_* tracking tables is not listed at all. The schema is listed under the new skipped_schemas key of the report, and a warning is logged.

A <filepath@> value is stored as written, so values like ./{schema}/sub/a.txt or {schema}//sub/b.txt read the same file as {schema}/sub/a.txt. In the root-level layout, GC normalizes every reference path (backslashes turned into /, then posixpath.normpath, leading / stripped) before matching it with the listed files, the same normalization <filepath@> uses for its reserved-section check. Such values now count as references to the files they read.

GC reads references only from the schemas it collects, in the store it collects. A file in a subfolder of {schema}/ that is not named as 0.14 named its files, and that only rows of other schemas reference (for example a <filepath@> file referenced from another schema), is still listed as an orphan. Pass every schema whose rows reference files in the store, such as a copy migrated with legacy_database, to the same collector, and preview with dry_run=True before deleting. The gc.py module, GarbageCollector, filepath_references and collect docstrings and both docs pages state the same; the class docstring's "pass any subset safely" and the docs "Multiple Schemas" section name the root-level layout as the exception.

On master, a table or column whose references cannot be read is logged and skipped, so files that only it references would be listed as orphans. #1689 records those failures as scan errors, which make collect(dry_run=False) refuse to delete. The <filepath@> references added here are read through the same method, so with #1689 a failed reference read, <filepath@> included, blocks deletion. This PR does not repeat that change.

Nothing changes for stores with a schema_prefix.

Other fixes

  • verify_external_integrity derives the path for blobs and attachments, whose filepath is NULL.
  • verify_schema_v20 escapes % in its LIKE pattern.
  • Docstring examples finalize with dry_run=False.

Behavior changes

  • A dry run now looks up every file, so every store must be configured and reachable even for a preview. This is one existence check per distinct file, which is one request per file on S3. Before, a dry run only counted rows. A store that is not configured raises an error.
  • Every referenced file is looked up before anything changes. If a reference has no tracking row or its file is missing, migrate_external, migrate_filepath, migrate_external_pointers_v2, and copy_table_data raise and change nothing. A dry run lists these references under problems.
  • migrate_external_pointers_v2 now changes the column type to JSON and renames the original to <name>_v1. Before, it wrote into the existing column.
  • create_parallel_schema runs with foreign key checks off for its session.
  • GarbageCollector.collect reports skipped_schemas. In the root-level layout it reads <filepath@> references of the collected schemas and keeps the files described above.

Not changed

  • migrate_external_pointers_v2 does not copy files (copy_files=True only logs a warning, as before).
  • GC still reads references only from the schemas it collects. In the root-level layout, a file in a subfolder of {schema}/, not named as 0.14 named its files, that only rows of other schemas reference can be listed as an orphan (see Garbage collection above).

Tests

  • tests/integration/test_migrate_legacy_external.py lays out a schema as 0.14 does, on MySQL. It has a ~external_legacy table, binary(16) columns with 0.14 comments and foreign keys, and files at 0.14's paths. The blobs were serialized by 0.14.6's own blob.pack. It checks that fetch1 returns the original arrays, attachment, and file after:
    • Option B, both from the _v2 columns and after finalizing, with nullability kept;
    • Option A: copy_table_data into a 2.x declaration, including where_clause and limit;
    • migrate_external_pointers_v2 on a binary(16) column with a new store name, and on a widened column;
    • each of these on a copy made by create_parallel_schema(..., copy_data=True), with legacy_database naming the original. Without it, each refuses and names legacy_database.
  • It also checks that:
    • create_parallel_schema copies the 0.14 schema with its foreign keys pointing into the copy, restores foreign key checks, and keeps % in comments;
    • with schema_prefix="", collect(dry_run=False) deletes no migrated 0.14 file: neither while the tracking table exists (the schema is skipped) nor after it is dropped (only an unrelated stray file is removed; a 0.14 blob no row references, and the blob migrated from 0.11, stay);
    • with schema_prefix="", collecting the original of a copy migrated with legacy_database, after the original's 0.14 tables are dropped, keeps every file only the copy references, and the copy still reads;
    • "subfolding": null finds the files at 0.14's default paths;
    • a repaired column stays nullable, the dry run says so, and the SQL statement above restores NOT NULL;
    • changed files fail the hash check;
    • a missing file stops each path with nothing changed or copied;
    • a column declared by 2.x, or by another codec, is refused;
    • comments with an apostrophe, %, and a backslash migrate and keep their text;
    • a failure on a later column drops the _v2 columns already written;
    • _v2 columns and finalized columns written by an earlier release are rewritten and then read;
    • finalize refuses when a _v2 column is missing;
    • a filepath row with a NULL contents_hash migrates;
    • a dry run needs the store configured.
  • The same file reads a migrated attachment through <attach@> on MySQL and PostgreSQL.
  • On MySQL and PostgreSQL, with schema_prefix="", <filepath@> values ./{db}/sub/a.txt and {db}//sub/b.txt are not listed as orphans, survive collect(dry_run=False), and still read. Without the normalization, both files are deleted.
  • On MySQL and PostgreSQL, with schema_prefix="", a copy's migrated-style values point under the original's name. Collecting the original alone keeps the 0.14-named file, the file directly under {schema}/, and the original's own <filepath@> file, and lists a file in a subfolder that only the copy's <filepath@> column references. Collecting both schemas keeps it; collect(dry_run=False) removes only a stray file, and every value still reads.
  • tests/unit/test_migrate_legacy_store.py covers the paths, the hash conversion, subfolding (including null), the attachment read, comment escaping, and the verify_schema_v20 query.
  • Against master: master has none of the 0.14 conversion helpers, so the new test module fails at import. Run against master's versions of the functions they exercise, the new tests fail: with master's gc.py, collect(dry_run=False) deletes the migrated 0.14 files (the two copy tests also fail with the gc.py of this PR's earlier commits, which did not keep files directly under {schema}/ or read <filepath@> references); with master's create_parallel_schema, copying the 0.14 schema fails with MySQL error 1824. The contents_hash and dry-run tests pin behavior that is correct in the new code paths.
  • The unit suite and the related integration files (test_gc.py, test_attach.py, test_hash_storage.py, test_object.py, test_blob.py, test_migrate_job_metadata.py) pass locally on MySQL and PostgreSQL.

Merge order

  1. fix(gc): refuse to delete when a reference scan fails #1689 (GC refuses to delete when a reference scan fails). It and this PR both change GarbageCollector._references; whichever merges second resolves the overlap, keeping the scan-error recording for the <filepath@> scan too.
  2. This PR.
  3. Docs: docs(migrate): finalize in-store migration with dry_run=False datajoint-docs#319, together with or after this PR's release.

For review

These are fail-closed choices I made. Please confirm or redirect:

  • Option A converts in-store columns inside copy_table_data, and migrate_external_pointers_v2 swaps the column in place. The alternative is to send Option A users to migrate_external on a copy of the schema, which now works with legacy_database.
  • The original database name is a parameter (legacy_database), not detected. A copy records nothing about its source, so the call refuses until the name is given.
  • create_parallel_schema turns foreign key checks off for its session instead of ordering tables by dependency.
  • In the root-level layout, GC skips a schema with 0.14 tracking tables entirely, and never removes 0.14-named files or files directly under {schema}/. Some files that are truly orphaned are therefore kept; they can be removed by hand.
  • A rerun rewrites unreadable _v2 columns by dropping and re-adding them. Their values came from the original columns, which are never changed.
  • Every referenced file is checked before anything changes, on a dry run too.

…fter migration

migrate_external wrote a url built from a NULL filepath, the 32-digit hex
hash, no path or store, and a :blob@store: comment that the heading does not
read. It now writes {hash, path, schema, store, size}, with the path where
0.14 stored the file and the same MD5 digest in base32, and a :<codec@store>:
comment. Migrated attachments record their name, which get_hash puts back
before checking the hash, as 0.14 hashed it.

migrate_filepath matches 0.14's binary(16) columns and writes filepath
metadata from the tracking table. migrate_external_pointers_v2 writes the
same metadata and refuses a binary(16) column. verify_external_integrity
derives blob and attachment paths, and verify_schema_v20 escapes % in LIKE.

Every referenced file is looked up before anything changes; a missing file
stops the migration with the schema unchanged.

Fixes #1698
…runs; keep NOT NULL

- copy_table_data converts 0.14 in-store columns when it copies a 0.14 table
  into a table declared by 2.x, so the guide's Option A works end to end.
  Every file is looked up first; a missing one stops the copy with nothing
  copied.
- migrate_external_pointers_v2 converts one 0.14 column in place: it adds the
  JSON column, fills it, and swaps it in with a 2.x comment that names
  dest_store. It refuses a column already declared by 2.x, pointing to
  copy_table_data.
- Column comments are quoted and escaped in every ALTER, including
  apostrophes, backslashes, and percent signs.
- If a step fails, the _v2 columns the run wrote are dropped again.
- A rerun rewrites _v2 columns written by an earlier release, whose values
  2.x cannot read, and repairs columns an earlier release finalized.
- Finalize checks every _v2 column first, renames both columns in one
  statement, and keeps the original nullability.
- Tests for each of these, for filepath rows without contents_hash, and for a
  dry run without the store configured.
… subfolding

- migrate_external, migrate_external_pointers_v2, copy_table_data and
  verify_external_integrity take legacy_database, the database 0.14 wrote
  the files from. 0.14 put blob and attachment files under that name, so a
  copy of a 0.14 schema is migrated by naming the original. copy_table_data
  defaults to the source schema. A refusal for missing files says so.
- create_parallel_schema creates and fills tables with foreign key checks
  off for the session, so tables that come before the tables they reference
  (such as the 0.14 ~external_* tables, which sort last) are created. Comments
  with % are copied.
- Garbage collection never deletes migrated 0.14 files in the root-level
  layout (schema_prefix=""): hash references count as covering, files named
  as 0.14 named blobs and attachments are never orphans, and the section of
  a schema that still has ~external_* tables is not listed.
- "subfolding": null, as the configuration template writes it, means unset,
  so 0.14's default (2, 2) applies.
- Repair keeps a column nullable when an earlier release's finalize made it
  so, says so in the dry run and the log, and the docstring explains how to
  restore NOT NULL.
…lepath@> references

In the root-level layout (schema_prefix=""), a file directly under {schema}/
is never an orphan: 2.x schema-addressed paths are {schema}/{table}/..., so such
a file is a 0.14 file stored without subfolding or a blob 0.14 had migrated from
0.11, which a copy migrated with legacy_database may reference. GC also reads
<filepath@> references of the collected schemas in this layout. The 0.14 file
rules and hash-reference coverage now apply only to the root-level layout.
A <filepath@> value is stored as written, so './{db}/sub/a.txt' and
'{db}//sub/b.txt' read the file at '{db}/sub/a.txt' but did not match the
listed path, and collect(dry_run=False) deleted it. Normalize every
reference path before matching (backslashes to '/', posixpath.normpath,
leading '/' stripped), as <filepath@> does for its reserved-section check.

Qualify the GarbageCollector *schemas docstring and the
filepath_references docstring for the root-level layout.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrating 0.14 externally stored blobs and attachments leaves columns that 2.x cannot read

1 participant